import { FontLoader } from 'three/examples/jsm/loaders/FontLoader';
import * as THREE from 'three';
import { TextGeometry } from 'three/examples/jsm/geometries/TextGeometry'

export const figureFontSize = 0.25
export const blockFontSize = 0.15
export const textFontSize = 0.15
export const textObjectHeight = 0.05
export const textColor = 0xffffff

export function loadFont(onFontLoaded) {
  // 加载字体文件
  const loader = new FontLoader();
  // 字体文件从three/examples/fonts复制到assets
  const fontFile = process.env.VITE_THREE_FONT_FILE
  loader.load(fontFile, onFontLoaded)
}

export function createTextObject(font, textStr, fontSize, fontHeight, fontColor, xPos, yPos, zPos) {
  const textGeometry = new TextGeometry(textStr, { font, size: fontSize, height: fontHeight });
  const textMaterial = new THREE.MeshPhongMaterial({ color: fontColor });
  const text = new THREE.Mesh(textGeometry, textMaterial);
  text.position.set(xPos, yPos, zPos);

  return text
}

export function createTextObjectOnBoxFront(font, textStr, xPos, vertical, boxDepth) {
  let textGeometry = new TextGeometry(textStr, { font, size: blockFontSize, height: textObjectHeight });
  if (vertical) {
    textGeometry.rotateZ(-Math.PI / 2)
  }
  let textMaterial = new THREE.MeshPhongMaterial({ color: textColor });
  let text = new THREE.Mesh(textGeometry, textMaterial);
  
  textGeometry.computeBoundingBox()
  const xSize = textGeometry.boundingBox.max.x - textGeometry.boundingBox.min.x
  const ySize = textGeometry.boundingBox.max.y - textGeometry.boundingBox.min.y
  text.position.set(xPos-xSize/2, ySize/2, boxDepth/2);

  textGeometry.computeBoundingBox()
  console.log('textGeometry.boundingBox', textGeometry.boundingBox)

  return text
}
